#include <unistd.h>
#include <fcntl.h>
#include <signal.h>

#include "Keypad.h"
#include "GuiContext.h"


Keypad::Keypad() : BasicDevice("/dev/keypad0"), buzzer(BuzzerSingleton::instance()), mpool(GuiContextSingleton::instance().getWorkPool()) {				//for key
}

Keypad::~Keypad() {

}


void Keypad::listen() {
    int oflags;
    signal(SIGIO, &Keypad::readKeyDevice);
    fcntl(deviceFd, F_SETOWN, getpid());
    oflags = fcntl(deviceFd, F_GETFL);
    fcntl(deviceFd, F_SETFL, oflags | FASYNC);
}



void Keypad::readKey(int sig) {

    KeyCode keyCode;

    read(deviceFd, &keyCode, sizeof(keyCode));


    Task* pTask = new Task{TaskType_t::DEFAULT_DATA, reinterpret_cast<void*> (keyCode)};
    mpool->pushPool(pTask);

    LOG_INFO("in signal:[%d],readKey:[%d]", sig, keyCode);

    buzzer.on();
    usleep(30*1000);
    buzzer.off();

}


void Keypad::readKeyDevice(int sig) {
    KeypadSingleton::instance().readKey(sig);
}





